package com.usian.wemedia.filter;

import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: WmTokenFilter
 * Author:   WGG
 * Date:    2022-11-2022/11/16-19:49
 * Version: 1.0
 * Description:
 */
@Order(2)
@WebFilter(filterName = "WmTokenFilter",urlPatterns = "/*")
@Slf4j
public class WmTokenFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        //获取id
        String userId = request.getHeader("userId");
        log.info("=========================={}+++++++++++++++", userId);
        //如果id不为空，则存入ThreadLocal
        if (userId != null){
            WmUser wmUser = new WmUser();
            wmUser.setApUserId(Integer.parseInt(userId));
            WmThreadLocalUtils.setUser(wmUser);
        }
        filterChain.doFilter(request,response);

    }
}
